﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.Serialization;
using UtilPublic.ServerLoger;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using UtilLib;

namespace UtilPublic.Config
{
    /// <summary>
    /// 配置文件
    /// </summary>
    public static partial class ConfRead
    {
        /// <summary>
        /// 读取字典
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="Name">配置文件名称</param>
        /// <param name="confDic">配置</param>
        /// <param name="pathName">目录</param>
        /// <returns></returns>
        public static bool ReadJsonDict<T1, T2>(string Name, ref Dictionary<T1, T2> confDic, string pathName)
        {
            string ConfName = $"{pathName}{Name}.json";
            try
            {
                FileStream fs_PTL = new FileStream(ConfName, FileMode.Open);
                StreamReader sr_PTL = new StreamReader(fs_PTL, Encoding.UTF8);
                try
                {
                    var text = sr_PTL.ReadToEnd();
                    if (text.IsNullOrEmpty())
                    {
                        text = "";
                    }

                    confDic = Jsh.J2O<Dictionary<T1, T2>>(text);

                    loger.Info($"读取配置-> {ConfName,-40} <-成功 共{confDic.Count}项");
                }
                catch (Exception e)
                {
                    loger.Fatal($"读取配置-> {ConfName,-40} <-失败", e);
                    return false;
                }
                finally
                {
                    sr_PTL.Close();
                }

            }
            catch (Exception)
            {
                loger.Fatal($"读取配置-> {ConfName,-40} <-失败2");
                return false;
            }


            return true;
        }

    }
}